import request from '@/utils/request'
import Qs from 'qs'
import fileRequest from '@/api/system/requestHandle'
import {getToken} from '@/utils/auth'
import axios from 'axios'

const cardApi = {
  list: params => request({
    url: `card?${Qs.stringify(params)}`,
    method: 'get',
    data: { notLoading: true }
  }),
  getCardByPublisher: params => request({
    url: `api/getCardByPublisher?${Qs.stringify(params)}`,
    method: 'get',
    data: { notLoading: true }
  }),
  save: data => request({
    url: `card`,
    method: 'post',
    data
  }),
  update: data => request({
    url: `card`,
    method: 'put',
    data
  }),
  remove: id => request({
    url: `card/${id}`,
    method: 'delete',
  }),
  getById: id => request({
    url: `card/${id}`,
    method: 'get'
  }),
  //导入
  importExcel: data => {
    let file = new FormData();
    file.append('file', data);
    return fileRequest({
      method: 'post',
      url: '/card/importExcel',
      headers: {
        'Content-Type': 'multipart/form-data',
        "Authorization": getToken()
      },
      data: file
    })
  },
  // 导出
  exportExcel(data) {
    let baseURL = process.env.NODE_ENV == 'development' ? "/dev-api" : '';
    return new Promise(function(resolve, reject) {
        let config = {
            method: "post",
            url: `${baseURL}/card/exportExcel`,
            data,
            headers: {
                'Content-Type': "application/json",
                "Authorization": getToken()
            },
            responseType: 'blob'
        }
        resolve(axios(config));
    })
  },
}

export default cardApi
